package com.itheima.remark.service.impl;

import com.itheima.remark.dao.CommentMapper;
import com.itheima.remark.service.CommentService;
import com.itheima.pojo.Comment;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    /**
     * 查询全部列表
     * @return
     */
    @Override
    public List<Comment> findAll() {
        return commentMapper.selectAll();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public Comment findById(Integer id){
        return  commentMapper.selectByPrimaryKey(id);
    }


    /**
     * 增加
     * @param comment
     */
    @Override
    public void add(Comment comment){
        commentMapper.insert(comment);
    }


    /**
     * 修改
     * @param comment
     */
    @Override
    public void update(Comment comment){
        commentMapper.updateByPrimaryKey(comment);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        commentMapper.deleteByPrimaryKey(id);
    }


    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    @Override
    public List<Comment> findList(Map<String, Object> searchMap){
        Example example = createExample(searchMap);
        return commentMapper.selectByExample(example);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Comment> findPage(int page, int size){
        PageHelper.startPage(page,size);
        return (Page<Comment>)commentMapper.selectAll();
    }

    /**
     * 条件+分页查询
     * @param searchMap 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Comment> findPage(Map<String,Object> searchMap, int page, int size){
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<Comment>)commentMapper.selectByExample(example);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // body
            if(searchMap.get("body")!=null && !"".equals(searchMap.get("body"))){
                criteria.andLike("body","%"+searchMap.get("body")+"%");
           	}
            // images
            if(searchMap.get("images")!=null && !"".equals(searchMap.get("images"))){
                criteria.andLike("images","%"+searchMap.get("images")+"%");
           	}
            // username
            if(searchMap.get("username")!=null && !"".equals(searchMap.get("username"))){
                criteria.andLike("username","%"+searchMap.get("username")+"%");
           	}

            // id
            if(searchMap.get("id")!=null ){
                criteria.andEqualTo("id",searchMap.get("id"));
            }
            // user_phone
            if(searchMap.get("userPhone")!=null ){
                criteria.andEqualTo("userPhone",searchMap.get("userPhone"));
            }
            // 口味分数
            if(searchMap.get("tasteScore")!=null ){
                criteria.andEqualTo("tasteScore",searchMap.get("tasteScore"));
            }
            // 环境分数
            if(searchMap.get("environmentScore")!=null ){
                criteria.andEqualTo("environmentScore",searchMap.get("environmentScore"));
            }
            // 服务分数
            if(searchMap.get("serverScore")!=null ){
                criteria.andEqualTo("serverScore",searchMap.get("serverScore"));
            }
            // 食材分数
            if(searchMap.get("ingredientScore")!=null ){
                criteria.andEqualTo("ingredientScore",searchMap.get("ingredientScore"));
            }
            // 商家id
            if(searchMap.get("productId")!=null ){
                criteria.andEqualTo("productId",searchMap.get("productId"));
            }

        }
        return example;
    }

}
